package com.lh;

public class HaversineToDist {
    private static final double R=6371000;// 地球半径，单位:米
    public static Double haversine(double lati1, double long1, double lati2, double long2) {
        // 首先将角度转为弧度
        double lati1Rad = Math.toRadians(lati1);
        double long1Rad = Math.toRadians(long1);
        double lati2Rad = Math.toRadians(lati2);
        double long2Rad = Math.toRadians(long2);

        double deltaLate = lati2Rad - lati1Rad;
        double deltaLong = long2Rad - long1Rad;

        double a = Math.sin(deltaLate / 2) * Math.sin(deltaLate / 2) +
                Math.cos(lati1Rad) * Math.cos(lati2Rad) *
                        Math.sin(deltaLong / 2) * Math.sin(deltaLong / 2);
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

        return R * c;
    }
}
